/**
 * 
 */
package br.com.skynix.tcmc.util;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

import org.jasypt.util.password.BasicPasswordEncryptor;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * @author mgrb
 * 
 */
public class WebUtil {

	public static final String USER_FUNCIONALIDADES = "TSF_USER_ACCESS";

	public static String USER_UUID = "TSF_USER";

	public static String encryptPassword(String key) {
		BasicPasswordEncryptor encryptor = new BasicPasswordEncryptor();
		return encryptor.encryptPassword(key);
	}

	/**
	 * @param password
	 * @param password2
	 * @return
	 */
	public static boolean checkPassword(String plainPws, String encrypted) {
		return new BasicPasswordEncryptor().checkPassword(plainPws, encrypted);
	}

	public static String getMensage(String key) {
		ResourceBundle resource = ResourceBundle.getBundle("systemMessages",
				FacesContext.getCurrentInstance().getViewRoot().getLocale());
		String texto;
		try {
			texto = resource.getString(key);
		}
		catch (MissingResourceException exception) {
			texto = "??" + key + "??";
		}
		return texto;

	}

	/**
	 * @param labelKey
	 * @return
	 */
	public static String getLabel(String labelKey) {
		ResourceBundle resource = ResourceBundle.getBundle("appLabels",
				FacesContext.getCurrentInstance().getViewRoot().getLocale());
		String texto;
		try {
			texto = resource.getString(labelKey);
		}
		catch (MissingResourceException exception) {
			texto = "??" + labelKey + "??";
		}
		return texto;
	}

	@SuppressWarnings("unchecked")
	public static Object lookUpService(Class class1, ServletContext context) {
		ApplicationContext appContext = WebApplicationContextUtils
				.getWebApplicationContext(context);
		return appContext.getBean(class1);
	}
}
